
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

/*---------------------------------------------------------
   Name: Initialize
   Desc: First function called. Use to set up your entity
---------------------------------------------------------*/
function ENT:Initialize()

	--self.Entity:SetModel( "models/dav0r/tnt/tnt.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

    local phys = self.Entity:GetPhysicsObject()

	if (phys:IsValid()) then
		phys:Wake()
	end
end

/*---------------------------------------------------------
   Name: KeyValue
   Desc: Called when a keyvalue is added to us
---------------------------------------------------------*/
function ENT:Setup( force, model,recoil, reload, bangradius, effect, explodeoncontact)

	self.cannonForce = force
	self.model=model
	self.recoil=recoil
	self.reload=reload
	self.reloadtimer=0
	self.firing=false
	self.bangradius=bangradius
	self.effect=effect
	self.explodeoncontact=explodeoncontact
	
	self.Entity:SetModel( model ) 
	
	// Wot no translation :(
	self:SetOverlayText( "Force: " .. math.floor(force)..", Reload: "..math.floor(reload).."\nPush Radius:"..math.floor(bangradius))
	
end

function ENT:OnTakeDamage( dmginfo )

	self.Entity:TakePhysicsDamage( dmginfo )
	--self.Entity:SetHealth(self.Entity:Health()-dmginfo:GetDamage())
	--if(self.Entity:Health()<=0) then
	--self.Entity:Fire( "break", "", 0 )
    --self.Entity:Remove()
	--end
end

function ENT:StartFiring()
		 self.firing=true
		 self:FireShot()
end

function ENT:StopFiring()
		 self.firing=false
end

function ENT:Think()
    if(self.firing) then
	    self:FireShot()
	end
	if(self.reloadtimer>0) then
		--self:NextThink(CurTime()+self.reloadtimer)
		self.reloadtimer=self.reloadtimer-1
	end
end

function ENT:FireShot()


	if ( !self.Entity:IsValid() ) then Msg("Force Dynamite: Doesn't exist!\n") return end
	
	if(self.reloadtimer<=0) then
	
	self.reloadtimer=self.reload
	
	if(self.effect!="") then
	
		local effectdata = EffectData()
		effectdata:SetOrigin( self.Entity:GetPos() )
		util.Effect( self.effect, effectdata, true, true )
	
	end
	
	local props=ents.FindInSphere( self.Entity:GetPos(), self.bangradius )
	
	local trace={}
	trace.start=self.Entity:GetPos()
	trace.filter=self.Entity
	
	local physself=self.Entity:GetPhysicsObject()
	
    for i,p in ipairs(props) do
    
    	if(p!=self.Entity) then
    	
	    	--if entity has physics
	    	
	    	local phys=p:GetPhysicsObject()
	        if (phys:IsValid()) then
	        
		    	--trace to the prop
		    	
				trace.endpos=p:GetPos()
				
				local traceRes=util.TraceLine(trace)
				
				local hit=traceRes.Entity
				
				if(hit:IsValid()) then
				
				    if(hit==p) then
				    
				        local forceVector=(p:GetPos()-self.Entity:GetPos()):GetNormalized()
				        
						if(self.explodeoncontact) then
					    	p:SetKeyValue("physdamagescale","500000000000")
					    	--ent:SetHealth(10000)
					    	--ent:SetKeyValue("health","1000")
					    	--Msg("Exploding on impact\n")
						end
					    --ent:SetKeyValue("ExplodeRadius",""..self.bangradius)
					    --ent:SetKeyValue("ExplodeDamage",""..self.bangpower)
					    
						phys:ApplyForceCenter(forceVector*self.cannonForce)
						physself:ApplyForceCenter(forceVector*-1*self.cannonForce*self.recoil)
					
					end
				
				end
			end
		end
	end
	end
	
end


local function On( pl, ent )
    if ( !ent || ent == NULL ) then return end

	local etab = ent:GetTable()
	etab:StartFiring()
end

local function Off( pl, ent )
   	if ( !ent || ent == NULL ) then return end

	local etab = ent:GetTable()
	etab:StopFiring()
end

numpad.Register( "ForceDynamite_On", 	On )
numpad.Register( "ForceDynamite_Off", Off )